/*
 *  nwpu/2005/2005_1.c
 *
 *  Harry Wei <harryxiyou@gmail.com> (C) 2013
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAXLEN			(100)
#define FILE_IN1		"D:\\test\\a.txt"
#define FILE_IN2		"D:\\test\\b.txt"
#define FILE_OUT		"D:\\test\\c.txt"

typedef struct _seqlist {
	int data[MAXLEN];
	int size;
};

typedef struct _seqlist seqlist;
typedef struct _seqlist* pseqlist;

int loaddata(char *fname, pseqlist la) {
	FILE *fp = NULL;
	int temp = 0;
	int i = 0;

	printf("999 enter func loaddata\n");
	printf("fname: %s, la: %p\n", fname, la);
	fp = fopen(fname, "r");
	if (NULL == fp) {
		fprintf(stderr, "%s\n", "fopen file %s error!\n");
		return -1;
	}
	while (!feof(fp)) {
		fscanf(fp, "%d\n", &temp);
		la->data[i++] = temp;
	}
	la->size = i;
	fclose(fp);
	printf("999 leave func loadata\n");
	return 0;
}

int outputdata(char *fname, seqlist la) {
	FILE *fp = NULL;
	int i = 0;

	fp = fopen(fname, "wt");
	for (i = 0; i < la.size; i++) {
		fprintf(fp, "%d\n", la.data[i]);
	}
	fclose(fp);
	return 0;
}

int mergelist(seqlist la, seqlist lb, pseqlist lc) {
	int i = 0;
	int j = 0;
	
	printf("enter func mergelist.\n");
	for (i = 0; i < la.size; i++) {
		lc->data[i] = la.data[i];
	}
	for (j = 0; j < lb.size; j++) {
		lc->data[i] = lb.data[j];
		i += 1;
	}
	lc->size = la.size + lb.size;
	return 0;
}

int debug(pseqlist debug) {
	int i = 0;
	for (i = 0; i < debug->size; i++) {
		printf("%d\n", debug->data[i]);
	}
	return 0;
}

int main(int argc, char *argv[]) {
	seqlist la;
	seqlist lb;
	seqlist lc;

#if 1
	memset(&la, 0, sizeof(struct _seqlist));
	memset(&lb, 0, sizeof(struct _seqlist));
	memset(&lc, 0, sizeof(struct _seqlist));
#endif
	printf("999 before loaddata\n");
	loaddata(FILE_IN1, &la);
	printf("999 after loaddata\n");
	loaddata(FILE_IN2, &lb);
	debug(&la);
	debug(&lb);
	mergelist(la, lb, &lc);
	debug(&lc);
	outputdata(FILE_OUT, lc);
	printf("leave main func\n");
	return 0;
}